package com.xzx.mr.inputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/7/20 21:24
 */
public class WholeRecorderReader extends RecordReader<Text, BytesWritable> {

    // 因为处理的是文件
    private FileSplit split;
    private Configuration configuration;
    private Text k = new Text();
    private BytesWritable v = new BytesWritable();
    private boolean processable = true;

    @Override
    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
        this.split = (FileSplit) split;
        this.configuration = context.getConfiguration();
    }

    /**
     * 返回true才会进map
     * 因为文件没有切块，这里一个文件只需要进来一次即可
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {
        if (processable) {

            // 获取切片路径，因为文件没有切，所以就是处理的文件文件路径
            Path path = split.getPath();
            // 获取到文件系统对象
            FileSystem fileSystem = path.getFileSystem(configuration);

            FSDataInputStream inputStream = fileSystem.open(path);

            //这里认为文件不会大
            byte[] buf = new byte[(int) split.getLength()];
            // 将文件读到buf中
            IOUtils.readFully(inputStream, buf, 0, buf.length);
            IOUtils.closeStream(inputStream);

            // 构建key与value的值
            k.set(path.toString());
            v.set(buf, 0 , buf.length);

            processable = false;

            return true;
        }
        return false;
    }

    @Override
    public Text getCurrentKey() throws IOException, InterruptedException {
        return k;
    }

    @Override
    public BytesWritable getCurrentValue() throws IOException, InterruptedException {
        return v;
    }

    @Override
    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }

    @Override
    public void close() throws IOException {

    }
}
